package com.shanghaionstar.utils;

import android.content.Context;

import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.common.util.DeviceUtil;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.LanguageType;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormatUtil {
    private final static String TAG = "DateFormatUtil";
    private static LogManager logger = LogManager.getInstance();

    /**
     * Format ovd Email date
     *
     * @param context
     * @param jsonCalendar
     * @return
     */
    public static String formatOvdEmail(Context context, String jsonCalendar) {
        if (jsonCalendar == null) {
            return "----";
        }
        String str = "";
        String dateString = jsonCalendar.replace("T", "");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        Date date = new Date();
        try {
            date = df.parse(dateString);
            date.setHours(date.getHours());
            StringBuilder formatStr = new StringBuilder();
            LanguageType language = LanguageType.fromValue(DeviceUtil.getLanguage());
            if (language == LanguageType.CHINESE) {
                formatStr.append("yyyy");
                formatStr.append(context.getString(R.string.year_unit));
                formatStr.append("MM");
                formatStr.append(context.getString(R.string.month_unit));
                formatStr.append("dd");
                formatStr.append(context.getString(R.string.date_unit));
                formatStr.append("HH:mm:ss");
            } else {
                formatStr.append("MM");
                formatStr.append(context.getString(R.string.month_unit));
                formatStr.append("dd");
                formatStr.append(context.getString(R.string.year_unit));
                formatStr.append("yyyy");
                formatStr.append(context.getString(R.string.date_unit));
                formatStr.append("HH:mm:ss");
            }
            df = new SimpleDateFormat(formatStr.toString());
            str = df.format(date);
        } catch (ParseException e) {
            logger.d(e.getMessage());
        }
        return str;
    }

    public static String format(Context context, String jsonCalendar) {
        if (jsonCalendar == null) {
            return "----";
        }
        String str = "";
        String dateString = jsonCalendar.replace("T", "");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        Date date = new Date();
        try {
            date = df.parse(dateString);
            date.setHours(date.getHours() + 8);
            StringBuilder formatStr = new StringBuilder();
            LanguageType language = LanguageType.fromValue(DeviceUtil.getLanguage());
            if (language == LanguageType.CHINESE) {
                formatStr.append("yyyy");
                formatStr.append(context.getString(R.string.year_unit));
                formatStr.append("MM");
                formatStr.append(context.getString(R.string.month_unit));
                formatStr.append("dd");
                formatStr.append(context.getString(R.string.date_unit));
                formatStr.append("HH:mm:ss");
            } else {
                formatStr.append("MM");
                formatStr.append(context.getString(R.string.month_unit));
                formatStr.append("dd");
                formatStr.append(context.getString(R.string.year_unit));
                formatStr.append("yyyy");
                formatStr.append(context.getString(R.string.date_unit));
                formatStr.append("HH:mm:ss");
            }
            df = new SimpleDateFormat(formatStr.toString());
            str = df.format(date);
        } catch (ParseException e) {
            logger.d(e.getMessage());
        }
        return str;
    }

    public static String format(Context context, String jsonCalendar, int hourOffet) {
        if (jsonCalendar == null) {
            return "----";
        }
        String str = "";
        String dateString = jsonCalendar.replace("T", "");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        Date date = new Date();
        try {
            date = df.parse(dateString);
            //always true
//            if ("false".equalsIgnoreCase(BusinessUtil.getConfigValue(FinalConfigParams.ISHOUROFFET, context))) {
//                hourOffet = 0;
//            }
            long time = date.getTime() + 3600000 * hourOffet;
            date = new Date(time);

            StringBuilder formatStr = new StringBuilder();
            LanguageType language = LanguageType.fromValue(DeviceUtil.getLanguage());
            if (language == LanguageType.CHINESE) {
                formatStr.append("yyyy");
                formatStr.append(context.getString(R.string.year_unit));
                formatStr.append("MM");
                formatStr.append(context.getString(R.string.month_unit));
                formatStr.append("dd");
                formatStr.append(context.getString(R.string.date_unit));
                formatStr.append("HH:mm:ss");
            } else {
                formatStr.append("MM");
                formatStr.append(context.getString(R.string.month_unit));
                formatStr.append("dd");
                formatStr.append(context.getString(R.string.year_unit));
                formatStr.append("yyyy");
                formatStr.append(context.getString(R.string.date_unit));
                formatStr.append("HH:mm:ss");
            }
            df = new SimpleDateFormat(formatStr.toString());
            str = df.format(date);
        } catch (ParseException e) {
            logger.d(e.getMessage());
        }
        return str;
    }

    public static Date format(String jsonCalendar) {
        String dateString = jsonCalendar.replace("T", "");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        Date date = new Date();
        try {
            date = df.parse(dateString);
        } catch (ParseException e) {
            logger.d(e.getMessage());
        }
        return date;
    }

    /*
    public static String format2(Context context, String jsonCalendar) {
        logger.i("format2 格式化日期 ---jsonCalendar:" + jsonCalendar);
        if (jsonCalendar == null) {
            return "----";
        }
        String str = "";
        String dateString = jsonCalendar.replace("T", "");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        Date date = new Date();
        try {
            date = df.parse(dateString);
            StringBuilder formatStr = new StringBuilder();
            formatStr.append("MM");
            formatStr.append(context.getString(R.string.month_unit));
            formatStr.append("dd");
            formatStr.append(context.getString(R.string.date_unit));
            formatStr.append("HH:mm");
            df = new SimpleDateFormat(formatStr.toString());
            str = df.format(date);
        } catch (ParseException e) {
            logger.d(e.getMessage());
        }
        return str;

    }
     */
    public static String format2(Context context, String jsonCalendar, int hourOffet) {
        logger.i("format2 格式化日期 ---jsonCalendar:" + jsonCalendar);
        if (jsonCalendar == null) {
            return "----";
        }
        String str = "";
        String dateString = jsonCalendar.replace("T", "");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        Date date = new Date();
        try {
            date = df.parse(dateString);
            if (!Constants.isHourOffet) {
                hourOffet = 0;
            }
            long time = date.getTime() + 3600000 * hourOffet;
            date = new Date(time);

//            StringBuilder formatStr = new StringBuilder();
//            formatStr.append("MM");
//            formatStr.append(context.getString(R.string.month_unit));
//            formatStr.append("dd");
//            formatStr.append(context.getString(R.string.date_unit));
//            formatStr.append("HH:mm:ss");
            df = new SimpleDateFormat("MM" + context.getString(R.string.month_unit) + "dd" + context.getString(R.string.date_unit) + "HH:mm:ss");
            str = df.format(date);
        } catch (ParseException e) {
            logger.d(e.getMessage());
        }
        return str;

    }

    public static String format3(Context context, String jsonCalendar, int hourOffet) {
        if (CommonUtil.isNull(jsonCalendar)) {
            return "----";
        }
        String str = "";
        String dateString = jsonCalendar.replace("T", "");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        Date date = new Date();
        try {
            date = df.parse(dateString);
            //always true
//            if ("false".equalsIgnoreCase(BusinessUtil.getConfigValue(FinalConfigParams.ISHOUROFFET, context))) {
//                hourOffet = 0;
//            }
            long time = date.getTime() + 3600000 * hourOffet;
            date = new Date(time);

            StringBuilder formatStr = new StringBuilder();
            LanguageType language = LanguageType.fromValue(DeviceUtil.getLanguage());
            if (language == LanguageType.CHINESE) {
                formatStr.append("MM");
                formatStr.append(context.getString(R.string.month_unit));
                formatStr.append("dd");
                formatStr.append(context.getString(R.string.date_unit));
                formatStr.append("HH:mm:ss");
            } else {
                formatStr.append("MM");
                formatStr.append(context.getString(R.string.month_unit));
                formatStr.append("dd");
                formatStr.append(context.getString(R.string.date_unit));
                formatStr.append("HH:mm:ss");
            }
            df = new SimpleDateFormat(formatStr.toString());
            str = df.format(date);
        } catch (ParseException e) {
            logger.d(e.getMessage());
        }
        return str;
    }

    public static String format2(Context context, int hourOffet) {
        String str = "";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        Date date = new Date();
        try {
            long time = date.getTime() + 3600000 * hourOffet;
            date = new Date(time);

            StringBuilder formatStr = new StringBuilder();
            formatStr.append("MM");
            formatStr.append(context.getString(R.string.month_unit));
            formatStr.append("dd");
            formatStr.append(context.getString(R.string.date_unit));
            formatStr.append("HH:mm:ss");
            df = new SimpleDateFormat(formatStr.toString());
            str = df.format(date);
        } catch (Exception e) {
            logger.d(e.getMessage());
        }
        return str;

    }

    public static String formatHHmm(String time, int hourOffset) {
        String str = "";
        String dateString = time.replace("T", "");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        Date date = new Date();
        try {
            date = df.parse(dateString);
            long offsetTime = date.getTime() + 3600000 * hourOffset;
            date = new Date(offsetTime);
            date.setHours(date.getHours());
            StringBuilder formatStr = new StringBuilder();
            formatStr.append("HH:mm");
            df = new SimpleDateFormat(formatStr.toString());
            str = df.format(date);
            System.out.println(str);
        } catch (Exception e) {

        }
        return str;
    }

    /**
     * 由于传入的是世界标准时间，与北京时间相差八个时区，转化后需要再加上八个小时
     *
     * @param cstTime    标准的北京时间格式，如：2016-10-09T09:30:07.281Z
     * @param hourOffset 时差
     * @return
     */
    public static String formatCST(String cstTime, int hourOffset) {
        LogManager.getInstance().d("formatCST:" + cstTime);
        String dateStr = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        try {
            Date date = sdf.parse(cstTime);
            long offsetTime = date.getTime() + 3600000 * hourOffset;
            date = new Date(offsetTime);
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            dateStr = sdf.format(date);
        } catch (Exception e) {
        }
        return dateStr;
    }

    public static String formatVehicleLocateTime(String jsonCalendar, int hourOffet) {
        if (jsonCalendar == null) {
            return "----";
        }
        String str = "";
        String dateString = jsonCalendar.replace("T", "");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        Date date = new Date();
        try {
            date = df.parse(dateString);
            long time = date.getTime() + +3600000 * hourOffet;
            date = new Date(time);
//            StringBuilder formatStr = new StringBuilder();
//            formatStr.append("yyyy");
//            formatStr.append("-");
//            formatStr.append("MM");
//            formatStr.append("-");
//            formatStr.append("dd");
//            formatStr.append(" ");
//            formatStr.append("HH:mm");
            df = new SimpleDateFormat("yyyy" + "-" + "MM" + "-" + "dd" + " " + "HH:mm");
            str = df.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

    /**
     * format 格式必须按照yyyy-MM-ddHH:mm:ss
     * 参考SimpleDateFormat:2017-11-03T00:00:00.000Z
     *
     * @param expireDate 过期时间
     * @return true 表示已经过期
     * @throws ParseException
     */
    public static boolean isOverDue(String expireDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        try {
            String curDate = sdf.format(new Date(System.currentTimeMillis()));
            // 将字符串形式的时间转化为Date类型的时间
            Date a = sdf.parse(expireDate);
            Date b = sdf.parse(curDate);
            // Date类的一个方法，如果a早于b返回true，否则返回false
            return a.before(b);
        } catch (ParseException e) {
        }
        return true;
    }
}
